/** Project 3
 *
 * @Group Members
 * 	Brentton Garber - mrbrentgarber@gmail.com
 * 	Georgii Tkachuk - georgii.t@gmail.com
 * 	Lucas Bridge - ltbridge@asu.edu
 * 	Austin Burnham - burnhamaustin@gmail.com
 * 	Kevin Greenberg - kgreenbe@asu.edu
 * @Revision
 *
 * @Since
 *
 */

#include "Print.h"
#include <iomanip>

const string symbolStrings[] = 
{
    "<no token>", "<IDENTIFIER>", "<NUMBER>", "<STRING>",
    "^","*","(",")","-","+","=","[","]",":",";",
    "<",">",",",".","/",":=","<=",">=","<>","..",
    "<END OF FILE>", "<ERROR>",
    "AND","ARRAY","BEGIN","CASE","CONST","DIV","DO","DOWNTO",
    "ELSE","END","FILE","FOR","FUNCTION","GOTO","IF","IN",
    "LABEL","MOD","NIL","NOT","OF","OR","PACKED","PROCEDURE",
    "PROGRAM","RECORD","REPEAT","SET","THEN","TO","TYPE","UNTIL",
    "VAR","WHILE","WITH",
};

/**
 * This is the constructor. It sets the fileName, and gets the time and date. It also initializes
 * lineCount, trueLineCound, and pageNnumber to zero.
 *
 * @param  &filename    the name of the file we are going to read.
 * @return none
 */
Print::Print(const string &filename)
{
	// save the filename to our private class variable 'fName'
	fName = filename;

	// create a new clock instance
	time_t clock = time(NULL);

	// save the date string to the private class variable 'date'
	date = ctime(&clock);

	// initialize the line count and page number to zero
	lineCount = 0;
    truelineCount = 0;
	pageNumber = 0;
}

/**
 * Destructor: this does not need to be used. Called when the class is being destroyed.
 */
Print::~Print()
{
}

/**
 * This prints one line of text from the input file. It first prints the line number and then
 * the text from the file.
 *
 * @param  &line    this string has the info
 * @return none
 */
void Print::PrintLine(const string &line)
{
	// Increase line count
	lineCount++;
    truelineCount++;
        
	// if this is the first page, or the amount of lines on the page exceeds the max, call PrintPageHeader();
	if(pageNumber == 0 || truelineCount / pageNumber > MAX_LINES_PER_PAGE)
	{
		PrintPageHeader();
	}

	// create the formatted line
	cout << right << std::setw(3)  << lineCount << ": " << line.c_str();

}

/**
 * This prints a page header that contains the page number, the date, time, and file name. 
 *
 * @param  none           
 * @return none
 */
void Print::PrintPageHeader()
{
	// increase the page number
	pageNumber++;

	// print the page header
	printf("\nPage %3i  %s  %s \n\n", pageNumber, fName.c_str(), date.c_str());
}

/**
 * Prints the information stored in the tokenizer class. On the left it prints what the 
 * tokenizer is and on the right it prints the value.
 *
 * @param  &tokenizer       This is the tokenizer that needs to be printed.
 * @return none
 */
void Print::PrintToken(Tokenizer &tokenizer)
{
    truelineCount++;
    
    if(pageNumber == 0 || truelineCount / pageNumber > MAX_LINES_PER_PAGE)
	{
		PrintPageHeader();
	}
    
	cout <<  "    >> " << left << std::setw(17) << symbolStrings[tokenizer.GetTokenCode()].c_str()<< tokenizer.GetTokenString().c_str() << endl;
    	
}
